using Mathmall.Administration.common;
using Mathmall.Administration.Service;
using Mathmall.Administration.Vo;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace Mathmall.Administration.Controllers;

[Route("/api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
    private readonly IUserService _userService;

    public UserController(IUserService userService)
    {
        _userService = userService;
    }

    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="userVo">用户</param>
    /// <returns></returns>
    [Authorize(Policy = "AddUser")]
    [HttpPost]
    public async Task<IdentityResult> AddUser(UserVo userVo)
    {
        return await _userService.AddUser(userVo);
    }

    /// <summary>
    /// 分页查询用户信息
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="condition"></param>
    /// <returns></returns>
    // [Authorize(Policy = "DeletePolicy")]
    // [Authorize(Roles = "admin")]
    [Authorize(Policy = "QueryUser")]
    [HttpGet]
    public async Task<ActionResult<PaginatedList<UserVo>>> PaginatedQuery(int pageIndex, int pageSize,
        string condition = "")
    {
        return await _userService.PaginatedQuery(pageIndex, pageSize, condition);
    }

    /// <summary>
    /// 给用户分配权限
    /// </summary>
    /// <param name="userId"></param>
    /// <param name="roleNames"></param>
    [Authorize(Policy = "AssignRole")]
    [HttpPost("{userId}")]
    public async Task<IdentityResult> DistributeRole(int userId, List<string> roleNames)
    {
        return await _userService.DistributeRole(userId, roleNames);
    }

    /// <summary>
    /// 根据用户id集合批量删除用户
    /// </summary>
    /// <param name="userIds"></param>
    /// <returns></returns>
    // [HttpDelete("[action]")]
    [Authorize(Policy = "DeleteUser")]
    [HttpDelete]
    public async Task<int> DeleteUsersByUserIds(List<int> userIds)
    {
        return await _userService.DeleteUsersByUserIds(userIds);
    }
}